<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { roleList, addRoles, updateRole, delRoles, saveOrUpdate, saveOrUpdateMini } from '@st/modules/permission/api/roles'
// import { EditPen, Delete, CirclePlus } from '@element-plus/icons-vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import FormRole from '@st/modules/permission/components/form-role/FormRole.vue'
import FormRoleMenu from '@st/modules/permission/components/form-role-menu/FormRoleMenu.vue'
import FormRoleMenuMini from '@st/modules/permission/components/form-role-menu-mini/FormRoleMenuMini.vue'
import { throttle } from '@/utils/system'

const fullscreenLoading = ref(false)

const searchForm = ref<any>({})

const handleSearch = () => {
  getLists()
}

const handleResetSearch = () => {
  searchForm.value = {}
  getLists()
}

// 获取列表
const loading = ref(false)
const tableData = ref<any[]>([])

const getLists = async () => {
  loading.value = true
  let res: any = await roleList(searchForm.value)
  loading.value = false
  tableData.value = res
}

const dialogFormVisible = ref(false)
const addEditData = ref<any>(null)
const addEditStatus = ref('add')
const roleFormRef = ref()

const handleAdd = () => {
  dialogFormVisible.value = true
  addEditData.value = null
  addEditStatus.value = 'add'
}

const handleEdit = (row: any) => {
  const { roleId, roleName, remark } = row
  dialogFormVisible.value = true
  addEditData.value = {
    roleId: `${roleId}`,
    roleName,
    remark
  }
  addEditStatus.value = 'edit'
}

const handleDialogFormSubmit = throttle(() => {
  roleFormRef.value.submitForm().then((data: any) => {
    if (data) {
      fullscreenLoading.value = true
      if (addEditStatus.value === 'add') {
        addRoles(data)
          .then(() => {
            dialogFormVisible.value = false
            ElMessage({
              type: 'success',
              message: '新增成功'
            })
            getLists()
          })
          .finally(() => {
            fullscreenLoading.value = false
          })
      } else {
        updateRole(data)
          .then(() => {
            dialogFormVisible.value = false
            ElMessage({
              type: 'success',
              message: '更新成功'
            })
            getLists()
          })
          .finally(() => {
            fullscreenLoading.value = false
          })
      }
    }
  })
})

const handleDialogFormClose = () => {
  dialogFormVisible.value = false
}

const handleDelete = (row: any) => {
  ElMessageBox.confirm('确定删除角色?', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    autofocus: false
  }).then(() => {
    fullscreenLoading.value = true
    const { roleId } = row
    delRoles({ roleId })
      .then(() => {
        ElMessage({
          message: '删除成功!',
          type: 'success'
        })
        getLists()
      })
      .finally(() => {
        fullscreenLoading.value = false
      })
  })
}

const drawerMenuVisible = ref(false)
const roleId = ref<any>(null)
const roleMenuRef = ref()

const selectMenu = (row: any) => {
  roleId.value = `${row.roleId}`
  drawerMenuVisible.value = true
}

const handleMenuDrawerSubmit = () => {
  const data = roleMenuRef.value.submitForm()
  const keys: any = data.map((v: any) => {
    return {
      roleId: roleId.value,
      menuId: v
    }
  })
  if (keys && keys.length) {
    fullscreenLoading.value = true
    saveOrUpdate(keys)
      .then(() => {
        drawerMenuVisible.value = false
        ElMessage({
          message: '保存成功!',
          type: 'success'
        })
      })
      .finally(() => {
        fullscreenLoading.value = false
      })
  } else {
    ElMessage.error('权限勾选不能为空!')
  }
}

const handleMenuDrawerClose = () => {
  drawerMenuVisible.value = false
}

const drawerMenuMiniVisible = ref(false)
const roleMenuMiniRef = ref()

const selectMiniMenu = (row: any) => {
  roleId.value = `${row.roleId}`
  drawerMenuMiniVisible.value = true
}

const handleMenuMiniDrawerSubmit = () => {
  const data = roleMenuMiniRef.value.submitForm()
  const keys: any = data.map((v: any) => {
    return {
      roleId: roleId.value,
      menuId: v
    }
  })
  if (keys && keys.length) {
    fullscreenLoading.value = true
    saveOrUpdateMini(keys)
      .then(() => {
        drawerMenuMiniVisible.value = false
        ElMessage({
          message: '保存成功!',
          type: 'success'
        })
      })
      .finally(() => {
        fullscreenLoading.value = false
      })
  } else {
    ElMessage.error('权限勾选不能为空!')
  }
}

const handleMenuMiniDrawerClose = () => {
  drawerMenuMiniVisible.value = false
}

onMounted(() => {
  getLists()
})
</script>

<template>
  <BasePage v-loading.fullscreen="fullscreenLoading" searchbar>
    <template #title>角色管理</template>
    <template #toolbar>
      <el-button type="primary" @click="handleAdd">
        <template #icon>
          <SvgIcon icon="plain-add"></SvgIcon>
        </template>
        新增
      </el-button>
    </template>
    <template #searchbar>
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="关键字：">
          <el-input v-model="searchForm.roleName" placeholder="请输入关键字" />
        </el-form-item>
        <el-form-item>
          <el-button type="info" text bg @click="handleResetSearch">
            <template #icon>
              <SvgIcon icon="plain-reset"></SvgIcon>
            </template>
            重置
          </el-button>
          <el-button type="primary" @click="handleSearch">
            <template #icon>
              <SvgIcon icon="plain-search"></SvgIcon>
            </template>
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </template>
    <el-table v-loading="loading" :data="tableData" class="custom-table" style="width: 100%" height="98%">
      <el-table-column type="index" label="编号" align="center" header-align="center" width="80"></el-table-column>
      <el-table-column prop="roleName" label="名称" min-width="180" show-overflow-tooltip> </el-table-column>
      <el-table-column prop="remark" label="描述" min-width="200" show-overflow-tooltip> </el-table-column>
      <el-table-column prop="createTime" label="创建时间" min-width="160"> </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" min-width="160"> </el-table-column>
      <el-table-column label="操作" min-width="260">
        <template #default="scope">
          <el-button type="primary" link @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="primary" link @click="selectMenu(scope.row)">分配Web权限</el-button>
          <el-button type="primary" link @click="selectMiniMenu(scope.row)">分配小程序权限</el-button>
          <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
          <!-- <div class="icon-text-box">
            <div class="icon-text info" @click.stop="handleEdit(scope.row)">
              <el-icon class="icon"><EditPen /></el-icon>
              <div class="text">编辑</div>
            </div>
            <div class="icon-text info" @click.stop="selectMenu(scope.row)">
              <el-icon class="icon"><CirclePlus /></el-icon>
              <div class="text add">分配Web权限</div>
            </div>
            <div class="icon-text info" @click.stop="selectMiniMenu(scope.row)">
              <el-icon class="icon"><CirclePlus /></el-icon>
              <div class="text add">分配小程序权限</div>
            </div>
            <div class="icon-text delete" @click.stop="handleDelete(scope.row)">
              <el-icon class="icon"><Delete /></el-icon>
              <div class="text">删除</div>
            </div>
          </div> -->
        </template>
      </el-table-column>
    </el-table>
  </BasePage>
  <BaseDialog v-model="dialogFormVisible">
    <template #title>{{ addEditStatus === 'add' ? '新增' : '编辑' }}</template>
    <template #buttons>
      <el-button type="info" text bg @click="handleDialogFormClose">取消</el-button>
      <el-button type="primary" @click="handleDialogFormSubmit">确认</el-button>
    </template>
    <FormRole ref="roleFormRef" :data="addEditData" :status="addEditStatus"></FormRole>
  </BaseDialog>
  <BaseDrawer v-model="drawerMenuVisible">
    <template #title>分配Web权限</template>
    <template #footer>
      <el-button type="info" text bg @click="handleMenuDrawerClose">取消</el-button>
      <el-button type="primary" @click="handleMenuDrawerSubmit">确认</el-button>
    </template>
    <FormRoleMenu ref="roleMenuRef" :role-id="roleId"></FormRoleMenu>
  </BaseDrawer>
  <BaseDrawer v-model="drawerMenuMiniVisible">
    <template #title>分配小程序权限</template>
    <template #footer>
      <el-button type="info" text bg @click="handleMenuMiniDrawerClose">取消</el-button>
      <el-button type="primary" @click="handleMenuMiniDrawerSubmit">确认</el-button>
    </template>
    <FormRoleMenuMini ref="roleMenuMiniRef" :role-id="roleId"></FormRoleMenuMini>
  </BaseDrawer>
</template>

<style lang="scss" scoped>
.icon-text-box {
  display: flex;
  justify-content: center;
  align-items: center;

  .icon-text {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 24px;
    cursor: pointer;
    .icon {
      margin-right: 5px;
      font-size: 16px;
    }
    .text {
      width: unset;
    }
  }
  .icon-text:last-child {
    margin-right: 0;
  }
  .icon-text.info {
    color: var(--theme-color);
  }
  .icon-text.delete {
    color: #f56c6c;
  }
}
</style>
